package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.mian.data.MainDataDeductionRulesDetailsDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataNotGroupRulesDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataNotGroupRulesDetailsDTO;
import com.ctshk.rpc.system.entity.MainDataNotGroupRules;
import com.ctshk.rpc.system.entity.MainDataNotGroupRulesDetails;
import com.ctshk.rpc.system.mapper.MainDataNotGroupRulesDetailsMapper;
import com.ctshk.rpc.system.mapper.MainDataNotGroupRulesMapper;
import com.ctshk.rpc.system.req.main.data.*;
import com.ctshk.rpc.system.service.IMainDataNotGroupRulesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 不成团退费规则 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-26
 */
@DubboService
public class MainDataNotGroupRulesServiceImpl extends ServiceImpl<MainDataNotGroupRulesMapper, MainDataNotGroupRules> implements IMainDataNotGroupRulesService {


    @Autowired
    private MainDataNotGroupRulesDetailsMapper mainDataNotGroupRulesDetailsMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(MainDataNotGroupRulesReq req, TokenUser tokenUser) {
        MainDataNotGroupRules mainDataNotGroupRules = EntityUtil.copy(req, MainDataNotGroupRules.class);
        long id = IdWorker.getId();
        LocalDateTime now = LocalDateTime.now();
        mainDataNotGroupRules.setIsDeleted(IsDeletedCode.NO.getCode());
        mainDataNotGroupRules.setId(id);
        mainDataNotGroupRules.setCreateId(tokenUser.getId());
        mainDataNotGroupRules.setGmtCreate(now);
        mainDataNotGroupRules.setModifiedId(tokenUser.getId());
        mainDataNotGroupRules.setGmtModified(now);
        baseMapper.insert(mainDataNotGroupRules);
        for (MainDataNotGroupRulesDetailsReq detailsReq:req.getRulesDetailsList()) {
            MainDataNotGroupRulesDetails rulesDetails = EntityUtil.copy(detailsReq, MainDataNotGroupRulesDetails.class);
            rulesDetails.setGroupId(id);
            rulesDetails.setCreateId(tokenUser.getId());
            rulesDetails.setCreateId(tokenUser.getId());
            mainDataNotGroupRulesDetailsMapper.insert(rulesDetails);
        }
        return Result.success(id);
    }

    @Override
    public PageResponse<MainDataNotGroupRulesDTO> queryList(MainDataNotGroupRulesQueryReq mainDataNotGroupRulesQueryReq) {
        IPage<MainDataNotGroupRules> page = new Page<MainDataNotGroupRules>(mainDataNotGroupRulesQueryReq.getPageNo(), mainDataNotGroupRulesQueryReq.getPageSize());
        QueryWrapper<MainDataNotGroupRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataNotGroupRules::getIsDeleted, 0).orderByDesc(MainDataNotGroupRules::getGmtModified);
        if (null != mainDataNotGroupRulesQueryReq.getStatus()) {
            queryWrapper.lambda().eq(MainDataNotGroupRules::getStatus, mainDataNotGroupRulesQueryReq.getStatus());
        }
        if (StringUtils.isNotBlank(mainDataNotGroupRulesQueryReq.getNotGroupRulesName())) {
            queryWrapper.lambda().like(MainDataNotGroupRules::getNotGroupRulesName, mainDataNotGroupRulesQueryReq.getNotGroupRulesName());
        }
        IPage<MainDataNotGroupRules> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataNotGroupRules> mainDataNotGroupRulesList = iPage.getRecords();
        if(CollectionUtils.isEmpty(mainDataNotGroupRulesList)){
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataNotGroupRulesDTO> notGroupRulesList = EntityUtil.copyList(mainDataNotGroupRulesList, MainDataNotGroupRulesDTO.class);
        for (MainDataNotGroupRulesDTO mainDataNotGroupRulesDTO:notGroupRulesList) {
            QueryWrapper<MainDataNotGroupRulesDetails> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MainDataNotGroupRulesDetails::getGroupId,mainDataNotGroupRulesDTO.getId());
            wrapper.lambda().orderByAsc(MainDataNotGroupRulesDetails::getBeforeDepartureDays);
            List<MainDataNotGroupRulesDetails> rulesDetails = mainDataNotGroupRulesDetailsMapper.selectList(wrapper);
            List<MainDataNotGroupRulesDetailsDTO> mainDataNotGroupRulesDTOS = EntityUtil.copyList(rulesDetails, MainDataNotGroupRulesDetailsDTO.class);
            mainDataNotGroupRulesDTO.setRulesDetailsList(mainDataNotGroupRulesDTOS);
        }
        return new PageResponse<>(notGroupRulesList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(MainDataNotGroupRulesIdReq req, TokenUser tokenUser) {
        MainDataNotGroupRules mainDataNotGroupRules = new MainDataNotGroupRules();
        mainDataNotGroupRules.setModifiedId(tokenUser.getId());
        mainDataNotGroupRules.setGmtModified(LocalDateTime.now());
        mainDataNotGroupRules.setId(req.getId());
        mainDataNotGroupRules.setIsDeleted(IsDeletedCode.YES.getCode());
        baseMapper.updateById(mainDataNotGroupRules);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(MainDataNotGroupRulesUpdateReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        QueryWrapper<MainDataNotGroupRulesDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataNotGroupRulesDetails::getGroupId,req.getId());
        mainDataNotGroupRulesDetailsMapper.delete(queryWrapper);
        for (MainDataNotGroupRulesDetailsReq detailsReq:req.getRulesDetailsList()) {
            MainDataNotGroupRulesDetails rulesDetails = EntityUtil.copy(detailsReq, MainDataNotGroupRulesDetails.class);
            rulesDetails.setGroupId(req.getId());
            rulesDetails.setCreateId(tokenUser.getId());
            rulesDetails.setModifiedId(tokenUser.getId());
            rulesDetails.setGmtModified(now);
            rulesDetails.setGmtCreate(now);
            mainDataNotGroupRulesDetailsMapper.insert(rulesDetails);
        }
        MainDataNotGroupRules mainDataNotGroupRules = EntityUtil.copy(req, MainDataNotGroupRules.class);
        mainDataNotGroupRules.setModifiedId(tokenUser.getId());
        mainDataNotGroupRules.setGmtModified(now);
        baseMapper.updateById(mainDataNotGroupRules);
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(MainDataNotGroupRulesIdReq mainDataNotGroupRulesIdReq, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataNotGroupRules mainDataNotGroupRules = baseMapper.selectById(mainDataNotGroupRulesIdReq.getId());
        MainDataNotGroupRules notGroupRules = new MainDataNotGroupRules();
        notGroupRules.setGmtModified(now);
        notGroupRules.setModifiedId(tokenUser.getId());
        if (mainDataNotGroupRules.getStatus() == 0) {
            notGroupRules.setId(mainDataNotGroupRulesIdReq.getId());
            notGroupRules.setStatus(1);
        }
        if (mainDataNotGroupRules.getStatus() == 1) {
            notGroupRules.setId(mainDataNotGroupRulesIdReq.getId());
            notGroupRules.setStatus(0);
        }
        baseMapper.updateById(notGroupRules);
    }

    @Override
    public MainDataNotGroupRulesDTO query(MainDataNotGroupRulesIdReq req) {
        MainDataNotGroupRules mainDataNotGroupRules = baseMapper.selectById(req.getId());
        MainDataNotGroupRulesDTO mainDataNotGroupRulesDTO = EntityUtil.copy(mainDataNotGroupRules, MainDataNotGroupRulesDTO.class);
        QueryWrapper<MainDataNotGroupRulesDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataNotGroupRulesDetails::getGroupId,req.getId());
        queryWrapper.lambda().orderByAsc(MainDataNotGroupRulesDetails::getBeforeDepartureDays);
        List<MainDataNotGroupRulesDetails> details = mainDataNotGroupRulesDetailsMapper.selectList(queryWrapper);
        List<MainDataNotGroupRulesDetailsDTO> dtos = EntityUtil.copyList(details, MainDataNotGroupRulesDetailsDTO.class);
        mainDataNotGroupRulesDTO.setRulesDetailsList(dtos);
        return mainDataNotGroupRulesDTO;
    }
}
